#ifndef __CLIENTTHREAD_H__
#define __CLIENTTHREAD_H__

// Qt Includes
#include <QThread>
#include <QQueue>

// Qt Declarations
class QTcpSocket;
class QReadWriteLock;
class QTimer;

// Local Declarations
class CTeaMakerMsg;

// ************************************************************
// CClientThread
// ************************************************************
class CClientThread : public QThread
{
	Q_OBJECT

public:
// Enumerations
	enum ClientThreadState
	{
		IdleState = 0,
		ConnectedState
	};

// Construction & Destruction
	CClientThread(QObject *const parent = NULL);
	~CClientThread();

// Member Functions
	ClientThreadState getThreadState() const;

signals:
	void socketDescriptorSet(int nSocketDescriptor);
	void newState(CClientThread::ClientThreadState nState);
	void stateChanged();
	
public slots:
	void setSocketDescriptor(int nSocketDescriptor);

protected:
// Implementation
	void run();

private slots:
	void setSocketDescriptorThreadSafe(int nSocketDescriptor);
	void processIncomingData();
	void socketDisconnected();

	void tickThread();

private:
// Member Functions
	void setCurrentState(ClientThreadState newState);

// Member Variables
	QTcpSocket *m_pTcpSocket;
	quint32 m_nDataSize;
	ClientThreadState m_CurrentState;
	QReadWriteLock *m_pStateLock;
	QQueue<CTeaMakerMsg*> m_qMessagesIn, m_qMessagesOut;
	QTimer *m_pTickTimer;	
};

#endif // #ifndef __CLIENTTHREAD_H__
